import axios from "axios";
import { ElMessage } from "element-plus";
import router from "@/router";

const baseURL = 'http://10.96.181.5:8000'

const instance = axios.create({
    baseURL: baseURL,
    timeout: 1000
})

instance.interceptors.request.use(
    function (config) {
        return config
    }, function (error) {
        return Promise.reject(error)
    }

)

instance.interceptors.response.use(
    function (response) {
        return response
    }, function (error) {
        if (error.response.data.code === 401) {
            ElMessage.warning('请先登录！')
            router.push('/login')
        }
        else { ElMessage.error(error.response.data.message || 'response 异常') }
        return Promise.reject(error)
    }
)

export default instance